package com.usefullc.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usefullc.common.enums.UserStatusEnum;
import com.usefullc.common.enums.UserTypeEnum;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.service.AbstractBizService;
import com.usefullc.user.domain.*;
import com.usefullc.user.mapper.EmployeeMapper;
import com.usefullc.user.param.EmployeeEditParam;
import com.usefullc.user.param.UserRoleParam;
import com.usefullc.user.query.EmployeeQuery;
import com.usefullc.user.vo.EmployeeVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: Sampson
 * @date: 2022-01-09 09:58
 */
@Service
public class EmployeeService extends AbstractBizService<EmployeeMapper,Employee,EmployeeQuery> {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;


    @Override
    protected QueryWrapper<Employee> getQueryWrapper(EmployeeQuery query) {
        QueryWrapper<Employee> queryWrapper = super.getQueryWrapper(query);
        return queryWrapper;
    }

    public Pager<EmployeeVo> findVoByPage(EmployeeQuery query){

        Pager<Employee> pager = this.findByPage(query);
        Pager<EmployeeVo> newPager = new Pager<>(pager);
        List<Employee> dataList = pager.getDataList();
        List<EmployeeVo> newDataList = new ArrayList<>();
        newPager.setDataList(newDataList);
        for (Employee employee : dataList) {
            User user = userService.findById(employee.getUserId());
            EmployeeVo employeeVo = BeanUtils.beanCopy(user, EmployeeVo.class);
            employeeVo.setId(employee.getId());
            employeeVo.setJobNo(employee.getJobNo());
            employeeVo.setPhone(employee.getPhone());
            employeeVo.setUserId(user.getId());


//            if(user.getDept() != null){
//                employeeVo.setDeptId(user.());
//            }
//            if(user.getPosition() != null){
//                employeeVo.setPositionId(user.getPosition().getId());
//            }
            List<Role> roleList = userService.getBaseMapper().findRoleListByUserId(user.getId());
            if(CollectionUtils.isNotEmpty(roleList)){
                List<Long> roleIds = roleList.stream().map(data -> data.getId()).collect(Collectors.toList());
                employeeVo.setRoleIds(roleIds);
            }
            newDataList.add(employeeVo);
        }
        return newPager;
    }


    @Transactional
    public void saveEmployee(EmployeeEditParam param){
        User user = BeanUtils.beanCopy(param, User.class);
        user.setId(param.getUserId());
        user.setType(UserTypeEnum.ADMIN.getCode());

//        if(param.getPositionId() != null){
//            Position position = new Position();
//            position.setId(param.getPositionId());
//            user.setPosition(position);
//        }
//
//        if(param.getDeptId() != null){
//            Dept dept = new Dept();
//            dept.setId(param.getDeptId());
//            user.setDept(dept);
//        }
        user.setStatus(UserStatusEnum.PASS.getCode());
        userService.saveUser(user);

        Employee employee = new Employee();
        employee.setId(param.getId());
        employee.setJobNo(param.getJobNo());
        employee.setName(param.getUserName());
        employee.setPhone(param.getPhone());
        employee.setUserId(user.getId());
        this.save(employee);

        //先删除
        userService.getBaseMapper().deleteUserRoleByUserId(user.getId());

        List<Long> roleIds = param.getRoleIds();
        if (CollectionUtils.isNotEmpty(roleIds)) {
            List<UserRoleParam> userRoleParams = new ArrayList<>();
            for (Long roleId : roleIds) {
                UserRoleParam userRoleParam = new UserRoleParam();
                userRoleParam.setRoleId(roleId);
                userRoleParam.setUserId(user.getId());
                userRoleParams.add(userRoleParam);
            }
            userService.getBaseMapper().insertUserRoleBatch(userRoleParams);
        }

    }





}
